package cfgs.gestudent.finestra.curs;


import javax.swing.DefaultComboBoxModel;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.GridBagConstraints;

import javax.swing.JButton;

import java.awt.Insets;

import javax.swing.JComboBox;

import cfgs.gestudent.dao.assignatures.MostrarAssignatura;
import cfgs.gestudent.dao.curs.InsertarImpartirAssignatura;
import cfgs.gestudent.dao.curs.MostrarCursos;
import cfgs.gestudent.dao.curs.MostrarDies;
import cfgs.gestudent.dao.grup.ObtenirGrup;
import cfgs.gestudent.preferencies.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ResourceBundle;

import javax.swing.JPanel;
import javax.swing.JTextField;

public class AfegirHorariAssignaturaCurs extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4794871247229435817L;
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	
	private MostrarCursos curs = null;
	private ObtenirGrup grup = null;
	private MostrarAssignatura assignatura= null;
	
	private JComboBox comboGrup;
	private JComboBox comboCurs;
	private JComboBox comboBoxAssignatura;
	
	private String cursSelec = null;
	private int idAssign = 0;
	private JTextField txtInici;
	private JTextField txtFi;
	
	private JComboBox comboDia = null;
	private JPanel panel = null;
	
	private InsertarImpartirAssignatura impartir = null;
	private int idGrup = 0;
	
	/**
	 * Create the frame.
	 */
	public AfegirHorariAssignaturaCurs() {
		setBackground(Color.WHITE);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {30, 0, 30, 30, 146, 30, 30, 30, 0, 30};
		gridBagLayout.rowHeights = new int[] {30, 0, 30, 0, 30, 0, 30, 30, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		setLayout(gridBagLayout);
		
		JLabel lblCurs = new JLabel(rb.getString("lblCurs"));
		GridBagConstraints gbc_lblCurs = new GridBagConstraints();
		gbc_lblCurs.insets = new Insets(0, 0, 5, 5);
		gbc_lblCurs.gridx = 1;
		gbc_lblCurs.gridy = 1;
		add(lblCurs, gbc_lblCurs);
		
		comboCurs = new JComboBox();
		curs = new MostrarCursos();
		curs.llistesComboBox();
		comboCurs.setModel(new DefaultComboBoxModel(curs.getLlista().toArray()));
		
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.gridwidth = 4;
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 3;
		gbc_comboBox.gridy = 1;
		add(comboCurs, gbc_comboBox);
		
		
		/**
		 * Grup_Curs
		 */
		JLabel lblGrup = new JLabel(rb.getString("lblGrup"));
		GridBagConstraints gbc_lblGrup = new GridBagConstraints();
		gbc_lblGrup.insets = new Insets(0, 0, 5, 5);
		gbc_lblGrup.gridx = 1;
		gbc_lblGrup.gridy = 3;
		add(lblGrup, gbc_lblGrup);
		
		comboGrup = new JComboBox();
		
		GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
		gbc_comboBox_1.gridwidth = 4;
		gbc_comboBox_1.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_1.gridx = 3;
		gbc_comboBox_1.gridy = 3;
		comboGrup.disable();
		add(comboGrup, gbc_comboBox_1);
		/**
		 * listener comboCurs
		 */
		grup = new ObtenirGrup();
		comboCurs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int index = comboCurs.getSelectedIndex();
				int idCursTabla = curs.getLlistaIdIndex(index);
				
				cursSelec = comboCurs.getSelectedItem().toString();
				grup = new ObtenirGrup();
				grup.llistesComboBoxId(idCursTabla);
				comboGrup.setModel(new DefaultComboBoxModel(grup.getLlista().toArray()));
				comboGrup.enable();
			}
		});
		
		/**
		 * Assignatura
		 */
		JLabel lblAssignatura = new JLabel(rb.getString("lblAssignatura"));
		GridBagConstraints gbc_lblAssignatura = new GridBagConstraints();
		gbc_lblAssignatura.insets = new Insets(0, 0, 5, 5);
		gbc_lblAssignatura.gridx = 1;
		gbc_lblAssignatura.gridy = 5;
		add(lblAssignatura, gbc_lblAssignatura);
		
		
		comboBoxAssignatura = new JComboBox();
		GridBagConstraints gbc_comboBox_2 = new GridBagConstraints();
		gbc_comboBox_2.gridwidth = 4;
		gbc_comboBox_2.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_2.gridx = 3;
		gbc_comboBox_2.gridy = 5;
		comboBoxAssignatura.disable();
		add(comboBoxAssignatura, gbc_comboBox_2);
		
		
		/**
		 * listener comboGrup
		 */
		assignatura = new MostrarAssignatura();
		comboGrup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				assignatura = new MostrarAssignatura();
				int indexgrup = comboGrup.getSelectedIndex();
				idGrup = grup.getLlistaIdIndex(indexgrup);
				comboBoxAssignatura.setModel(new DefaultComboBoxModel(assignatura.llistesComboGrup(idGrup).toArray()));
				comboBoxAssignatura.enable();
			}
		});
		
			
		/**
		 * panell inferior
		 */
		panel = new JPanel();
		panel.setBackground(Color.white);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridheight = 2;
		gbc_panel.gridwidth = 7;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 7;
		add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] {0, 98, 30, 0, 30, 135, 30};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		/**
		 * Hora inici
		 */
		JLabel lblHoraInici = new JLabel(rb.getString("horaIni"));
		GridBagConstraints gbc_lblHoraInici = new GridBagConstraints();
		gbc_lblHoraInici.anchor = GridBagConstraints.EAST;
		gbc_lblHoraInici.insets = new Insets(0, 0, 5, 5);
		gbc_lblHoraInici.gridx = 0;
		gbc_lblHoraInici.gridy = 0;
		panel.add(lblHoraInici, gbc_lblHoraInici);
		
		txtInici = new HintTextField("HH:MM:SS");
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		panel.add(txtInici, gbc_textField);
		txtInici.setColumns(10);
		
		/**
		 * dia
		 */
		JLabel lblDia = new JLabel(rb.getString("dia"));
		GridBagConstraints gbc_lblDia = new GridBagConstraints();
		gbc_lblDia.insets = new Insets(0, 0, 5, 5);
		gbc_lblDia.gridx = 3;
		gbc_lblDia.gridy = 0;
		panel.add(lblDia, gbc_lblDia);
		
		comboDia = new JComboBox();
		MostrarDies dies = new MostrarDies();
		comboDia.setModel(new DefaultComboBoxModel(dies.llistesComboBox().toArray()));
		GridBagConstraints gbc_comboDia = new GridBagConstraints();
		gbc_comboDia.insets = new Insets(0, 0, 5, 5);
		gbc_comboDia.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboDia.gridx = 5;
		gbc_comboDia.gridy = 0;
		panel.add(comboDia, gbc_comboDia);
		
		/**
		 * Hora fi
		 */
		JLabel lblHoraFi = new JLabel(rb.getString("horaFi"));
		GridBagConstraints gbc_lblHoraFi = new GridBagConstraints();
		gbc_lblHoraFi.anchor = GridBagConstraints.EAST;
		gbc_lblHoraFi.insets = new Insets(0, 0, 0, 5);
		gbc_lblHoraFi.gridx = 0;
		gbc_lblHoraFi.gridy = 2;
		panel.add(lblHoraFi, gbc_lblHoraFi);
		
		txtFi = new HintTextField("HH:MM:SS");
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.insets = new Insets(0, 0, 0, 5);
		gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_1.gridx = 1;
		gbc_textField_1.gridy = 2;
		panel.add(txtFi, gbc_textField_1);
		txtFi.setColumns(10);
		
		JButton btnGuardar = new JButton(rb.getString("btnGuardar"));
		btnGuardar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean segui2 = false;
				String campsIncorrectes = "";
				if (comboBoxAssignatura.getSelectedItem() != null) {
					segui2 = true;
					if(txtInici.getText()==null | txtInici.getText().equals("")){
						segui2 = false;
					}else{
						segui2 = true;
						if(txtFi.getText()==null | txtFi.getText().equals("")){
							segui2 = false;
						}else{
							segui2 = true;
							if(txtInici.getText().matches("[0-9][0-9]:[0-9][0-9]:[0-9][0-9]")){
								segui2 = true;
								if(txtFi.getText().matches("[0-9][0-9]:[0-9][0-9]:[0-9][0-9]")){
									segui2 = true;
								}else{
									segui2 = false;
									JOptionPane.showMessageDialog(null,
											rb.getString("missatgeHoraFiIncorrecta")+campsIncorrectes,
											rb.getString("titolHoraIncorrecta"),
											JOptionPane.ERROR_MESSAGE);
								}
							}else{
								segui2 = false;
								JOptionPane.showMessageDialog(null,
										rb.getString("missatgeHoraIniciIncorrecta")+campsIncorrectes,
										rb.getString("titolHoraIncorrecta"),
										JOptionPane.ERROR_MESSAGE);
							}
						}
					}
				}else{
					segui2 = false;
				}
				
				
				
				
				
				
				
				

				if (segui2 == true) {
					int index1 = comboBoxAssignatura.getSelectedIndex();

					idAssign = assignatura.getLlistaId_2().get(index1);

					String horaInici = txtInici.getText();
					String horaFi = txtFi.getText();
					String dies = comboDia.getSelectedItem().toString();

					impartir = new InsertarImpartirAssignatura();
					boolean seguir = impartir.inserirDades(idGrup, idAssign,
							horaInici, horaFi, dies);
					if (seguir == true) {
						// Missatge les dades han estat insertades
						JOptionPane.showMessageDialog(null,
								rb.getString("missatgeDadesInsertades"),
								rb.getString("titolDadesInsertades"),
								JOptionPane.INFORMATION_MESSAGE);
						txtInici.setText(null);
						txtFi.setText(null);
					} else {
						// Missatge les dades han estat insertades
						JOptionPane.showMessageDialog(null,
								rb.getString("missatgeDadesNoInsertades"),
								rb.getString("titolDadesNoInsertades"),
								JOptionPane.WARNING_MESSAGE);
						txtInici.setText(null);
						txtFi.setText(null);
					}
				} else {
					JOptionPane.showMessageDialog(null,
							rb.getString("faltenCampsObligatoris"),
							rb.getString("titolFaltenCamps"),
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		JButton btnCancelar = new JButton(rb.getString("btnCancelar"));
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtInici.setText(null);
				txtFi.setText(null);
			}
		});
		
		GridBagConstraints gbc_btnCancelar = new GridBagConstraints();
		gbc_btnCancelar.insets = new Insets(0, 0, 0, 5);
		gbc_btnCancelar.gridx = 6;
		gbc_btnCancelar.gridy = 9;
		add(btnCancelar, gbc_btnCancelar);
		
		GridBagConstraints gbc_btnGuardar = new GridBagConstraints();
		gbc_btnGuardar.gridx = 8;
		gbc_btnGuardar.gridy = 9;
		add(btnGuardar, gbc_btnGuardar);
		
	}

}
